<template>
    <div>
        <r-search :searchHandle="searchHandleAdd" />
        <r-search :searchForm="searchForm" :searchHandle="searchHandle" :searchData="searchData" />
        <r-table :isSelection="true" :isIndex="true" :isPagination="true" :tableData="tableData" :tableCols="tableCols" :tablePage="tablePage" @refresh="init()"></r-table>
        <r-form :formRules="formRules" :formHandle="formHandle" :formData="formData" :formCols="formCols" ref="elForm" labelWidth="200px" dialogWidth="1000px" :isHandle="true" />
    </div>
</template>
<script>
export default {
    data() {
        var checkpass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.formDataEditPsw.password) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            searchData: {
                activeName: '',
                startTime: '',
                endTime: '',
                status: '',
            },
            searchForm: [
                {
                    type: 'input',
                    prop: 'activeName',
                    placeholder: '活动名称',
                    width: 170
                },
                {
                    type: 'date',
                    prop: 'startTime',
                    placeholder: '活动开始时间',
                    width: 190
                },
                {
                    type: 'date',
                    prop: 'endTime',
                    placeholder: '活动结束时间',
                    width: 190
                },
                {
                    type: 'select',
                    prop: 'status',
                    placeholder: '状态',
                    width: 150,
                    options: [
                        {
                            label: '上架',
                            value: '1'
                        },
                        {
                            label: '下架',
                            value: '2',
                        },
                    ]
                },
            ],
            searchHandleAdd: [
                {
                    label: "新增活动",
                    type: "success",
                    icon: "el-icon-circle-plus-outline",
                    handle: e => this.elFormVisible()
                },
            ],
            searchHandle: [
                {
                    label: '搜索',
                    type: 'success',
                    icon: 'el-icon-search',
                    handle: e => this.init()
                },
                {
                    label: '重置',
                    icon: 'el-icon-refresh-right',
                    handle: e => this.searchFromReset()
                }
            ],
            tableData: [
            ],
            tableCols: [
                {
                    label: '活动名称',
                    prop: 'activeName',
                    // theme: e => e.status ? 'primary' : 'danger', type: "tag",
                },
                {
                    label: '活动周期开始时间',
                    prop: 'startTime',
                    width: 100,
                    formatter: e => e.startTime.split(' ')[0],

                },
                {
                    label: '活动周期结束时间',
                    prop: 'endTime',
                    formatter: e => e.endTime.split(' ')[0],
                    width: 100,
                },
                {
                    label: '设备数量',
                    prop: 'equipmentCount'
                },
                {
                    label: '商品数量',
                    prop: 'goodsCount'
                },
                {
                    label: '状态',
                    prop: 'status',
                    theme: e => e.status == '2' ? 'danger' : 'primary',
                    formatter: e => e.status == '2' ? '下架' : '上架',
                    type: "tag",
                },
                {
                    label: '创建时间',
                    prop: 'ctime',
                    width: 160,
                    formatter: e => e.ctime && this.getDeta(+new Date(e.ctime.replace(/T/g, ' ')))
                },
                {
                    label: "操作",
                    type: "button",
                    width: 400,
                    btnList: [
                        {
                            label: '编辑',
                            type: 'primary',
                            size: 'mini',
                            // isShow: e => e.orderId == '123' ? true : false,
                            handle: (row) => this.elFormDetail(row)
                        },
                        {
                            label: '删除',
                            type: 'primary',
                            size: 'mini',
                            handle: (row) => this.TableDel('/active', { id: row.id }, 'del')
                        },
                        {
                            label: '商品管理',
                            type: 'primary',
                            size: 'mini',
                            handle: (row) => this.$router.push({ path: '/discount-goodsMg', query: { id: row.id } })
                        },
                        {
                            label: '设备管理',
                            type: 'primary',
                            size: 'mini',
                            handle: (row) => this.$router.push({ path: '/discount-equipmentMg', query: { id: row.id } })
                        },
                    ]
                },

            ],
            tablePage: { limit: 10, page: 1, total: 0 },
            formData: {
                activeName: "",
                startTime: "",
                endTime: "",
                status: ""
            },
            formCols: [
                { type: "input", label: "活动名称", placeholder: '请输入活动名称', width: 350, prop: "activeName", },
                { type: "datet", label: "活动周期开始时间", placeholder: '请选择开始时间', width: 350, prop: "startTime", },
                { type: "datet", label: "活动周期结束时间", placeholder: '请选择结束时间', width: 350, prop: "endTime", },
                { type: "select", label: "状态", placeholder: '请选择状态', width: 350, prop: "status", options: [{ label: '启用', value: 1 }, { label: '禁用', value: 2 }] },
            ],
            formRules: {
                activeName: [{ required: true, message: '请输入活动名称', trigger: 'blur' },],
                startTime: [{ required: true, message: '请选择开始时间', trigger: 'blur' },],
                endTime: [{ required: true, message: '请选择结束时间', trigger: 'blur' },],
                status: [{ required: true, message: '状态', trigger: 'blur' }],
            },
            formHandle: [
                {
                    label: "取消",
                    handle: e => this.elFormVisible(),
                },
                {
                    label: "确定",
                    type: "success",
                    handle: e => this.elFormSubmit(),
                },

            ],

        };
    },
    components: {},
    computed: {},
    mounted() {
        this.init()
    },
    methods: {
        async init() {
            this.tablePage.offset = this.tablePage.page
            let param = { ...this.searchData, ...this.tablePage }
            // console.log(param);
            let res = await this.api.activelist(param)
            if (res) {
                this.tablePage.total = res.data.total
                this.tableData = res.data.list
                // this.tableData.map(item => {
                // item.startTime = this.getDeta(+new Date(item.startTime.replace(/T/g, ' ')))
                // item.endTime = this.getDeta(+new Date(item.startTime.replace(/T/g, ' ')))
                // })
            }
        },
        async elFormSubmit() {
            this.formData.account = this.formData.phone
            if (this.formData.id) {
                let res = await this.api.activeupdate(this.formData)
                if (res) {
                    this.formData.id = ''
                    this.elFormVisible()
                    this.init()
                }
            } else {
                let res = await this.api.activeadd(this.formData)
                if (res) {
                    this.elFormVisible()
                    this.init()
                }
            }

        },
        elFormDetail(row) {
            Object.keys(this.formData).forEach(key => {
                this.formData[key] = row[key]
            })
            this.formData.id = row.id;
            this.formData.password = ''
            this.elFormVisible()
        },

    }
};
</script>
<style scoped lang='scss'>
.el-icon-search {
  margin-right: 10px;
}
.Tip {
  margin-left: 20px;
}
</style>
